

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;




public class ConfigExtracter
{	
//	private Player player;
//	private Vector<Fighter> fighters;
//	Vector<ParticleCluster> clusters;
//	private Vector<StaticObject> statObjects;
//	private Vector<AbstractObject> abstractObjects;
	private Document dom;
	
//	public Player getPlayer()
//	{
//		return player;
//	}

	
	public ConfigExtracter()
	{
	}
	
	
	
	public void parseFile(String path) {
		
		//parse the xml file and get the dom object
		parseXmlFile(path);
		
		//get each employee element and create a Employee object
		parseGame();
		
		//Iterate through the list and print the data
		//printData();
		
	}
	
	
	private void parseXmlFile(String path){
		//get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		
		try {
			
			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();
			
			//parse using builder to get DOM representation of the XML file
			dom = db.parse(path);
			

		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}


	////////////////////////////////////////////////////////////////////////////////////////////// PARSE SIMPLE elements

	////////////////////////////////////////////////////////////////////////////////////// PARSE HEALTH
	private float parseHealth(Element el)
	{
		return (float)getIntValue(el, "health"); 
	}
	////////////////////////////////////////////////////////////////////////////////////// PARSE MASS
	private float parseMass(Element el)
	{
		return (float)getIntValue(el, "mass"); 
	}
	////////////////////////////////////////////////////////////////////////////////////// PARSE TYPE
	private String parseType(Element el)
	{
		return getTextValue(el,"type"); 
	}

	////////////////////////////////////////////////////////////////////////////////////// PARSE RADIUS
	private float parseRadius(Element el)
	{
		return (float)getIntValue(el, "radius"); 
	}
	//////////////////////////////////////////////////////////////////////////////////////PARSE NAME
	private String parseName(Element ele)
	{
		return getTextValue(ele, "name");
	}

	////////////////////////////////////////////////////////////////////////////////////// PARSE COORDINATES
	private Vector parseCoord(Element el)
	{

		String input=getTextValue(el,"coord");
	    String[] segments = input.split(",");
	        
	    Vector vect= new Vector(Integer.parseInt(segments[0]),Integer.parseInt(segments[1]));
	    return vect;
	}	
	
	////////////////////////////////////////////////////////////////////////////////////////////// PARSE COMPLEX elements

	//////////////////////////////////////////////////////////////////////////////////////PARSE GAME
	private void parseGame()
	{
		//TODO ... TBD
		//Game gm = engine.createGame();
		//get the root element
		Element docEle = dom.getDocumentElement();
		//get a nodelist of <level> elements
		NodeList nl = docEle.getElementsByTagName("level");
		System.out.println("found "+ nl.getLength()+ "levels");
		if(nl != null && nl.getLength() > 0) {
			for(int i = 0 ; i < nl.getLength();i++) {
				
				//get the level element
				Element el = (Element)nl.item(i);
				
				//get the level object
				//Level lvl = parseLevel(el);
				
				//add it to list
				//gm.addLevel(lvl);
			}
		}
		
		//get a nodelist of <GameOptions> elements

		//add the element to the respective list
		//abstractObjects.add(gm);

	}	
	//////////////////////////////////////////////////////////////////////////////////////PARSE LEVEL
	private void parseLevel(Element el)
	{
		//Level lvl = engine.createLevel();

		//parse the player
		//Player player=
		parsePlayer(el);

		//parse the stars
		NodeList nl = el.getElementsByTagName("star");
		System.out.println("found "+ nl.getLength()+ "stars");
		if(nl != null && nl.getLength() > 0) {
			for(int i = 0 ; i < nl.getLength();i++) {
				
				//get the star element
				Element elem = (Element)nl.item(i);
				
				//get the star object
				//Star str = 
				parseStar(elem);
				//statObjects.add((StaticObject)str);
				//add it to list
				//lvl.addStar(str);
				
			}
		}
		
		//return lvl;


	}	
	//////////////////////////////////////////////////////////////////////////////////////PARSE PLAYER
	private void parsePlayer(Element el)
	{
		System.out.println("parsePlayer");
		//read the name
		String name = parseName(el);
		//		("mass");
		float mass = parseMass(el);
		//		("type");
		String type = parseType(el);
		//		("coord");
		Vector coord = parseCoord(el);
		//		("radius");
		float radius = parseRadius(el);
			
		//		("health");
		float health = parseHealth(el);
		
		
		///Player p= engine.createPlayer(name, (int)coord.x, (int)coord.y, mass, radius, 200, health);
		//myCore.addPlayer(p);
		
		//System.out.println("parsePlayer CHECKING PLAYER RADIUS  " + p.getRadius());
		
		//return p;
	}
	

	////////////////////////////////////////////////////////////////////////////////////// PARSE STAR
	private void parseStar(Element el)
	{
		System.out.println("parseStar");
		//read the name
		String name = parseName(el);
		//		("mass");
		float mass = parseMass(el);
		//		("type");
		String type = parseType(el);
		//		("coord");
		Vector coord = parseCoord(el);
		//		("radius");
		float radius = parseRadius(el);
		

		

		NodeList nl = el.getElementsByTagName("planet");

		if(nl != null && nl.getLength() > 0) {
			for(int i = 0 ; i < nl.getLength();i++) {
				//get the planet element
				Element elem = (Element)nl.item(i);
				//get the planet object
				parsePlanet(elem);
			}
		}
		
		//Star str= engine.createStar(name, (int)coord.x,(int)coord.y, mass, radius);
		//myCore.addStaticObject((StaticObject)str);

		//return str;
	}

	
	////////////////////////////////////////////////////////////////////////////////////// PARSE PLANET

	private void parsePlanet(Element el)
	{
		System.out.println("parsePlanet");
		//read the name
		String name = parseName(el);
		//		("mass");
		float mass = parseMass(el);
		//		("type");
		String type = parseType(el);
		//		("coord");
		Vector coord = parseCoord(el);
		//		("radius");
		float radius = parseRadius(el);
		
	
		

		//Planet plnt = engine.createPlanet(name, (int)coord.x,(int)coord.y, mass, radius);
		//myCore.addStaticObject((StaticObject)plnt);

		
		
		NodeList nl = el.getElementsByTagName("moon");
		//System.out.println("found "+ nl.getLength()+ "moons");
		if(nl != null && nl.getLength() > 0) {
			for(int i = 0 ; i < nl.getLength();i++) {
				
				Element elem = (Element)nl.item(i);
				parseMoon(elem);
			}
		}
	}
	
	////////////////////////////////////////////////////////////////////////////////////// PARSE MOON
	private void parseMoon(Element el)
	{
		System.out.println("parseMoon");
		//read the name
		String name = parseName(el);
		//		("mass");
		float mass = parseMass(el);
		//		("type");
		String type = parseType(el);
		//		("coord");
		Vector coord = parseCoord(el);
		//		("radius");
		float radius = parseRadius(el);
		
		//Moon mn = engine.createMoon(name, (int)coord.x,(int)coord.y, mass, radius);
		//myCore.addStaticObject((StaticObject)mn);
		//return mn;
	}	

	/////////////////////////////////////////////////////////////////////////////////////////////////// ELEMENT CONTENT GETTERS (STR AND INT)
	private String getTextValue(Element ele, String tagName) 
	{
		
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}
		return textVal;
	}
	private int getIntValue(Element ele, String tagName) 
	{
		//System.out.println(getTextValue(ele,tagName));
		return Integer.parseInt(getTextValue(ele,tagName));
	}
	

	
}




